package com.zhongxin.wymovieticket.adapter;

import java.util.List;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.NetworkImageView;
import com.zhongxin.wymovieticket.R;
import com.zhongxin.wymovieticket.R.id;
import com.zhongxin.wymovieticketentity.HotMovies;
import com.zhongxin.wymovieticketutils.AsyncImageLoader;
import com.zhongxin.wymovieticketutils.AsyncImageLoader.ImageLoaderlistener;
import com.zhoujing.utils.ImageDownLoad;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class HotMovieAdapter extends BaseAdapter {

	private List<HotMovies> hotMovieslist;
	private Context context;
	private RequestQueue queue;
	private AsyncImageLoader imageLoader;
	private String url2 = "http://flv.bn.netease.com/videolib3/1408/26/isfVg8137/HD/isfVg8137-mobile.mp4";

	public HotMovieAdapter(List<HotMovies> hotMovieslist, RequestQueue queue,
			Context context) {
		// TODO Auto-generated constructor stub
		this.hotMovieslist = hotMovieslist;
		this.context = context;
		this.queue = queue;
		imageLoader = new AsyncImageLoader(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return hotMovieslist.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return hotMovieslist.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.hotmovielistitem, parent, false);
			viewHolder.hotmovie_image = (NetworkImageView) convertView
					.findViewById(R.id.hotmovie_image);
			viewHolder.hotmovie_image.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent it = new Intent();
					it.setAction(Intent.ACTION_VIEW);
					it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					// 该地址随着传入的mp4的地址而改变
					Uri uri = Uri.parse(url2);// 此url就是流媒体文件的下载地址
					it.setDataAndType(uri, "video/*");// type的值是 "video/*"或者
														// "audio/*"
					context.startActivity(it);

				}
			});
			viewHolder.hotmovie_name = (TextView) convertView
					.findViewById(R.id.hotmovie_name);
			viewHolder.hotmovie_isnewmovie = (ImageView) convertView
					.findViewById(R.id.hotmovie_isnewmovie);
			viewHolder.hotmovie_grade = (TextView) convertView
					.findViewById(R.id.hotmovie_grade);
			viewHolder.hotmovie_highlight = (TextView) convertView
					.findViewById(R.id.hotmovie_highlight);
			viewHolder.hotmovie_screenings = (TextView) convertView
					.findViewById(R.id.hotmovie_screenings);
			viewHolder.hotmovie_buyticket = (Button) convertView
					.findViewById(R.id.hotmovie_buyticket);
			viewHolder.hotmovie_imagetag = (ImageView) convertView
					.findViewById(R.id.hotmovie_imagetag);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.hotmovie_name.setText(hotMovieslist.get(position).getName());
		if (hotMovieslist.get(position).getIsNew() == 1) {
			viewHolder.hotmovie_isnewmovie
					.setBackgroundResource(R.drawable.icon_new_tag);
		} else {

		}
		if (hotMovieslist.get(position).getDimensional().equals("3D,IMAX")) {
			viewHolder.hotmovie_imagetag
					.setBackgroundResource(R.drawable.icon_type_3dimax);
		} else if (hotMovieslist.get(position).getDimensional().equals("3D")) {
			viewHolder.hotmovie_imagetag
					.setBackgroundResource(R.drawable.icon_type_3d);
		} else if (hotMovieslist.get(position).getDimensional()
				.equals("2D,IMAX")) {
			viewHolder.hotmovie_imagetag
					.setBackgroundResource(R.drawable.icon_type_2dimax);
		}

		viewHolder.hotmovie_grade.setText(""
				+ hotMovieslist.get(position).getGrade() + "分");

		viewHolder.hotmovie_highlight.setText(hotMovieslist.get(position)
				.getHighlight());
		viewHolder.hotmovie_screenings.setText(hotMovieslist.get(position)
				.getScreenings());
		// if (hotMovieslist.get(position) != null &&
		// !hotMovieslist.get(position).getLogo().equals("")) {
		// viewHolder.hotmovie_image.setTag(hotMovieslist.get(position).getLogo());
		// Bitmap bitmap = imageLoader.downLoader(viewHolder.hotmovie_image, new
		// ImageLoaderlistener() {
		//
		// public void onImageLoader(Bitmap bitmap, ImageView imageView) {
		// if (hotMovieslist.get(position) != null &&
		// hotMovieslist.get(position).getLogo() != null &&
		// imageView.getTag().equals(hotMovieslist.get(position).getLogo())) {
		// imageView.setImageBitmap(bitmap);
		// if (bitmap != null) {
		// viewHolder.hotmovie_image.setImageBitmap(bitmap);
		// }
		// }
		// }
		// });
		//
		// }

		try {
			ImageDownLoad.downLoadImage(hotMovieslist.get(position).getLogo(),
					context, queue, viewHolder.hotmovie_image);
		} catch (Exception e) {
			// TODO: handle exception
		}

		return convertView;

	}

	private static class ViewHolder {
		NetworkImageView hotmovie_image;
		TextView hotmovie_name;
		ImageView hotmovie_isnewmovie;
		ImageView hotmovie_imagetag;
		TextView hotmovie_grade;
		TextView hotmovie_highlight;
		TextView hotmovie_screenings;
		Button hotmovie_buyticket;

	}

}
